﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace RayTracing
{
    class Onb
    {
        public Vector3 U { get; private set; }
        public Vector3 V { get; private set; }
        public Vector3 W { get; private set; }

        public const float OnbEpsilon = 0.01f;

        public static Onb FromU(Vector3 u)
        {
            var m = Vector3.UnitY;
            var n = Vector3.UnitX;
            var onb = new Onb();
            onb.U = Vector3.Normalize(u);
            var v = Vector3.Cross(m, onb.U);
            if (v.Length() < OnbEpsilon)
            {
                v = Vector3.Cross(n, onb.U);
            }
            onb.W = Vector3.Cross(onb.U, onb.V);
            return onb;
        }

        public static Onb FromUv(Vector3 u, Vector3 v)
        {
            var onb = new Onb();
            onb.U = Vector3.Normalize(u);
            onb.W = Vector3.Normalize(Vector3.Cross(u, v));
            onb.V = Vector3.Cross(onb.U, onb.W);
            return onb;
        }
    }
}
